package Util;

import java.text.SimpleDateFormat;
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.TimeZone;

public class FormatTime {

    //格式化时间差
    public static String formatTimeDifference(LocalDateTime targetTime) {
        LocalDateTime now = LocalDateTime.now();
        Duration duration = Duration.between(targetTime, now);
        long hours = duration.toHours();
        if(hours == 0){
            long minutes = duration.toMinutes();
            if(minutes == 0){
                long seconds = duration.getSeconds();
                if(seconds == 0){
                    return "刚刚";
                }else {
                    return seconds + "秒前";
                }
            }else {
                return minutes + "分钟前";
            }
        }
        if (hours < 24) {
            return hours + "小时前";
        }
        else {
            return targetTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        }
    }

      public static String formatDateTime(Date date){
          SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
          sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
          return sdf.format(date);
     }
    public static String formatDateTime(LocalDateTime date){
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        return date.format(formatter);
    }

 }



